import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:personinfo/model/home/SearchCarModel.dart';
import 'package:personinfo/model/home/UserModel.dart';
import 'package:personinfo/page/home/searchCarInfo/CarInfo.dart';
import 'package:personinfo/utils/ColorUtil.dart';
import 'package:personinfo/widgets/common/CommonWidget.dart';
import 'package:personinfo/widgets/common/keepAlive/KeepAliveWrapper.dart';



class MySearchCar extends StatefulWidget {
  Map arguments;
  MySearchCar({super.key,required this.arguments});
  @override
  State<StatefulWidget> createState() => _MySearchCar();
}

class _MySearchCar extends State<MySearchCar> with TickerProviderStateMixin {
  late TabController _tabController;
  late TabController _secondTabController; // 添加二级TabController
  List tabs = ["已发布","已过期",'已取消'];
  List secondTabs = ["全部", "未处理", "寻车中", "已完成"]; // 二级tabs

  // 添加构建二级tab视图的方法
  Widget _buildSecondLevelTabView() {
    return Container(
      decoration: BoxDecoration(
        color: Theme.of(context).colorScheme.onPrimary, // 设置背景色
      ),
      child: Column(
        children: [
          // 自定义TabBar，避免使用Scaffold
          Container(
              decoration: BoxDecoration(
                color: Colors.white,
                boxShadow: [
                  BoxShadow(
                    color: Colors.grey.withOpacity(0.2), // 浅灰，透明度 20%
                    blurRadius: 2.0,
                    offset: Offset(0, 2), // 向下偏移 2px
                  ),
                ],
              ),
            child: TabBar(
                controller: _secondTabController,
                padding: EdgeInsets.zero,
                isScrollable: false,
                labelStyle: TextStyle(color: ColorUtil.primaryBgColor),
                unselectedLabelColor: Colors.black,
                indicatorColor: Colors.transparent,
                tabs: secondTabs.map((e) =>
                    Container(
                      height: 44.0,
                      width: 100,
                      alignment: Alignment.center,
                      padding: EdgeInsets.symmetric(horizontal: 8),
                      child: Text(e, style: TextStyle(fontSize: 14)),
                    )
                ).toList()
            )
          ),
          Expanded(
            child: TabBarView(
              controller: _secondTabController,
              children: const [
                 CarInfo(orderNum: "X1111-全部",),
                 CarInfo(orderNum: "X2222-未处理",),
                 CarInfo(orderNum: "X3333-寻车中",),
                 CarInfo(orderNum: "X4444-已完成",),
              ],
            ),
          ),
        ],
      ),
    );
  }

  List get _pages {
    return [
      _buildSecondLevelTabView(),
      const CarInfo(orderNum: "X4444-已过期",),
      const CarInfo(orderNum: "X5555-已取消",)
  ];
}

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _tabController = TabController(length: tabs.length,initialIndex: widget.arguments['index'] ?? 0, vsync: this);
    _secondTabController = TabController(length: secondTabs.length,initialIndex: widget.arguments['subIndex'] ?? 0, vsync: this); // 初始化二级TabControlle
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return  Scaffold(
      appBar: commonWidget.appBar(
        title: const Text("我的寻车") ,
        leading: GestureDetector(onTap: (){Navigator.pop(context);},child: const Icon(Icons.keyboard_arrow_left),),
        actions: [
          GestureDetector(
            onTap: (){
              Navigator.pushNamed(context, '/publishSearchCar',arguments: new SearchCarModel());
            },
            child: const Padding(
              padding: EdgeInsets.only(right: 10),
              child: Icon(Icons.find_replace),
            ),
          )
        ],
        bottom: PreferredSize(
            preferredSize: Size.fromHeight(50),
            child: Container(
              decoration: BoxDecoration(
                color: Theme.of(context).colorScheme.onPrimary, // 添加背景色
              ),
              child: TabBar(
                controller: _tabController,
                padding: EdgeInsets.fromLTRB(1, 0, 1, 1),
                unselectedLabelColor: Colors.black,
                labelStyle: TextStyle(fontWeight: FontWeight.bold,color: Colors.black),
                indicatorColor:  ColorUtil.primaryBgColor,
                tabs: tabs.map((e) => SizedBox(width: 100,child: Tab(text: e,))).toList(),
              ),
            )
        ),
      ),
      body: TabBarView(
        //构建
        controller: _tabController,
        children: _pages.map((e) {
          return KeepAliveWrapper(
            child: Container(
              alignment: Alignment.center,
              child: e,
            ),
          );
        }).toList(),
      ),
    );
  }

  @override
  void dispose() {
    // 释放资源
    _tabController.dispose();
    super.dispose();
  }
}
